package webgamemaker.Editor;

import webgamemaker.Common.Callback;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.JSONParser;

public class ServerIO 
{
	public String phpDir;
	private static ServerIO instance;
	
	private ServerIO()
	{
	}
	
	public static ServerIO getInstance()
	{
		if(instance == null)
			instance = new ServerIO();
		return instance;
	}
	
	public void setPhpDir(String phpDir)
	{
		this.phpDir = phpDir;
	}
	
	public void readDir(String dir, final Callback callback)
	{
		RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, phpDir+"/readDir.php");
		builder.setHeader("Content-Type", "application/x-www-form-urlencoded");

		try {
	      builder.sendRequest(URL.encodeComponent("path")+"="+URL.encodeComponent(dir), new RequestCallback() {

			public void onResponseReceived(Request request, Response response) 
			{
				System.out.println(response.getText());
				callback.onCall(new DirData(JSONParser.parse(response.getText())));
			}

			public void onError(Request request, Throwable exception) 
			{
				
			}
	      });
	    } catch (RequestException e) 
	    {
	    }
	}
}
